#!/bin/sh
# Spacecast daemon init script.
. /etc/utils.sh

APP="scdaemon"
BINARY="/app/spacecast/daemon"
FLAGS="-logtostderr"

# Make sure we have a directory to store Monlog credentials.
mkdir -p /config/monlog

# Make sure we have a directory to store Widevine credentials.
mkdir -p /config/widevine

# Make sure we have directories to store bootstrap parameters.
mkdir -p /tmp/buffet
mkdir -p /tmp/monlog

running() {
  [ -n "$(pgrep -f "$BINARY")" ]
}

case "$1" in
  start)
    if running; then
      echo "SpaceCast daemon is already running!"
    else
      # Record free disk space in the system log
      df

      babysit_start 10 "$APP" "$BINARY" "$FLAGS"
    fi
    ;;
  stop)
    pkill -f "$BINARY"
    ;;
  restart|reload)
    $0 stop;$0 start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac
